from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
from uiDef import *
from musicPlayer import MusicPlayer
from videoPlayer import VideoPlayer
from clockTime import ClockTimeWidget
from weatherShow import WeatherShow
from dht11 import DHT11Widget
from usbCamera import CameraWidget
from mapShow import MapWidget
from carInfo import CarInfoWidget
import configparser,os


APP_BTN_SIZE = 100
APP_BTN_FONT = "font-size:25px; font-weight:bold; color:white"

g_bgIdx = 0

def init_config():
    config = configparser.ConfigParser()
    global g_bgIdx
    if os.path.exists('config.ini'):
        config.read('config.ini')
        value = config.get('section', 'bg_idx')
        g_bgIdx = value
        print("g_bgIdx:",g_bgIdx)
    else:
        config['section'] = {'bg_idx': 0}
        with open('config.ini', 'w') as configfile:
            config.write(configfile)
            print("write configfile")
            
def write_config(idx):
    try:
        config = configparser.ConfigParser()
        config['section'] = {'bg_idx': idx}
        with open('config.ini', 'w') as configfile:
            config.write(configfile)
    except Exception as e:
        print("Error reading INI file:", str(e))



# 主窗口
class mainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        global g_bgIdx
        #init_config()
        self.bgIdx = g_bgIdx
        print("self.bgIdx:",self.bgIdx)
        
        self.main_UI()
        self.button_UI()
        
    def main_UI(self):
        self.setFixedSize(WIN_WIDTH, WIN_HEIGHT)
        self.setWindowTitle("主窗口")
        
        self.mainWight = QWidget()
        self.mainLayout = QVBoxLayout()
        self.mainWight.setLayout(self.mainLayout)
        self.setCentralWidget(self.mainWight)
        
    def button_UI(self):
        # 各个APP启动按钮
        
        # 第一排
        # <<<音乐>>>
        self.musicAppBtn = QPushButton(self)
        self.musicAppBtn.setStyleSheet("QPushButton{border-image: url(resource/app_music.png)}")
        self.musicAppBtn.setFixedSize(APP_BTN_SIZE, APP_BTN_SIZE)
        self.musicAppLabel = QLabel(self)
        self.musicAppLabel.setText("音乐")
        self.musicAppLabel.setStyleSheet(APP_BTN_FONT)
        self.musicAppLabel.setAlignment(Qt.AlignCenter)
        self.vboxMusicApp = QVBoxLayout()
        self.vboxMusicApp.addWidget(self.musicAppBtn)
        self.vboxMusicApp.addWidget(self.musicAppLabel)
        # <<<视频>>>
        self.videoAppBtn = QPushButton(self)
        self.videoAppBtn.setStyleSheet("QPushButton{border-image: url(resource/app_video.png)}")
        self.videoAppBtn.setFixedSize(APP_BTN_SIZE, APP_BTN_SIZE)
        self.videoAppLabel = QLabel(self)
        self.videoAppLabel.setText("视频")
        self.videoAppLabel.setStyleSheet(APP_BTN_FONT)
        self.videoAppLabel.setAlignment(Qt.AlignCenter)
        self.vboxVideoApp = QVBoxLayout()
        self.vboxVideoApp.addWidget(self.videoAppBtn)
        self.vboxVideoApp.addWidget(self.videoAppLabel)
        # <<<时钟>>>
        self.clockAppBtn = QPushButton(self)
        self.clockAppBtn.setStyleSheet("QPushButton{border-image: url(resource/app_clock.png)}")
        self.clockAppBtn.setFixedSize(APP_BTN_SIZE, APP_BTN_SIZE)
        self.clockAppLabel = QLabel(self)
        self.clockAppLabel.setText("时钟")
        self.clockAppLabel.setStyleSheet(APP_BTN_FONT)
        self.clockAppLabel.setAlignment(Qt.AlignCenter)
        self.vboxClockApp = QVBoxLayout()
        self.vboxClockApp.addWidget(self.clockAppBtn)
        self.vboxClockApp.addWidget(self.clockAppLabel)
        # <<<天气>>>
        self.weatherAppBtn = QPushButton(self)
        self.weatherAppBtn.setStyleSheet("QPushButton{border-image: url(resource/app_weather.png)}")
        self.weatherAppBtn.setFixedSize(APP_BTN_SIZE, APP_BTN_SIZE)
        self.weatherAppLabel = QLabel(self)
        self.weatherAppLabel.setText("天气")
        self.weatherAppLabel.setStyleSheet(APP_BTN_FONT)
        self.weatherAppLabel.setAlignment(Qt.AlignCenter)
        self.vboxWeatherApp = QVBoxLayout()
        self.vboxWeatherApp.addWidget(self.weatherAppBtn)
        self.vboxWeatherApp.addWidget(self.weatherAppLabel)
        # <<<温湿度>>>
        self.dht11AppBtn = QPushButton(self)
        self.dht11AppBtn.setStyleSheet("QPushButton{border-image: url(resource/app_dht11.png)}")
        self.dht11AppBtn.setFixedSize(APP_BTN_SIZE, APP_BTN_SIZE)
        self.dht11AppLabel = QLabel(self)
        self.dht11AppLabel.setText("温湿度")
        self.dht11AppLabel.setStyleSheet(APP_BTN_FONT)
        self.dht11AppLabel.setAlignment(Qt.AlignCenter)
        self.vboxDHT11App = QVBoxLayout()
        self.vboxDHT11App.addWidget(self.dht11AppBtn)
        self.vboxDHT11App.addWidget(self.dht11AppLabel)
        
        # 第二排
        # <<<车前路况>>>
        self.cameraAppBtn = QPushButton(self)
        self.cameraAppBtn.setStyleSheet("QPushButton{border-image: url(resource/app_camera.png)}")
        self.cameraAppBtn.setFixedSize(APP_BTN_SIZE, APP_BTN_SIZE)
        self.cameraAppLabel = QLabel(self)
        self.cameraAppLabel.setText("车前路况")
        self.cameraAppLabel.setStyleSheet(APP_BTN_FONT)
        self.cameraAppLabel.setAlignment(Qt.AlignCenter)
        self.vboxCameraApp = QVBoxLayout()
        self.vboxCameraApp.addWidget(self.cameraAppBtn)
        self.vboxCameraApp.addWidget(self.cameraAppLabel)
        # <<<地图导航>>>
        self.mapAppBtn = QPushButton(self)
        self.mapAppBtn.setStyleSheet("QPushButton{border-image: url(resource/app_map.png)}")
        self.mapAppBtn.setFixedSize(APP_BTN_SIZE, APP_BTN_SIZE)
        self.mapAppLabel = QLabel(self)
        self.mapAppLabel.setText("地图导航")
        self.mapAppLabel.setStyleSheet(APP_BTN_FONT)
        self.mapAppLabel.setAlignment(Qt.AlignCenter)
        self.vboxMapApp = QVBoxLayout()
        self.vboxMapApp.addWidget(self.mapAppBtn)
        self.vboxMapApp.addWidget(self.mapAppLabel)
        # <<<车辆信息>>>
        self.carInfoAppBtn = QPushButton(self)
        self.carInfoAppBtn.setStyleSheet("QPushButton{border-image: url(resource/app_car.png)}")
        self.carInfoAppBtn.setFixedSize(APP_BTN_SIZE, APP_BTN_SIZE)
        self.carInfoAppLabel = QLabel(self)
        self.carInfoAppLabel.setText("车辆信息")
        self.carInfoAppLabel.setStyleSheet(APP_BTN_FONT)
        self.carInfoAppLabel.setAlignment(Qt.AlignCenter)
        self.vboxCarInfoApp = QVBoxLayout()
        self.vboxCarInfoApp.addWidget(self.carInfoAppBtn)
        self.vboxCarInfoApp.addWidget(self.carInfoAppLabel)

        # 更换背景图片按钮
        self.changeBgBtn = QPushButton(self)
        self.changeBgBtn.setStyleSheet("QPushButton{border-image: url(resource/ic_change_bg.png)}")
        self.changeBgBtn.setFixedSize(50, 50)
        self.changeBgBtn.move(WIN_WIDTH - 80, WIN_HEIGHT - 80)
        
        # 布局
        self.vboxAppBtnLine1 = QHBoxLayout()
        self.vboxAppBtnLine1.addStretch(1)
        self.vboxAppBtnLine1.addLayout(self.vboxMusicApp)
        self.vboxAppBtnLine1.addStretch(1)
        self.vboxAppBtnLine1.addLayout(self.vboxVideoApp)
        self.vboxAppBtnLine1.addStretch(1)
        self.vboxAppBtnLine1.addLayout(self.vboxClockApp)
        self.vboxAppBtnLine1.addStretch(1)
        self.vboxAppBtnLine1.addLayout(self.vboxWeatherApp)
        self.vboxAppBtnLine1.addStretch(1)
        self.vboxAppBtnLine1.addLayout(self.vboxDHT11App)
        self.vboxAppBtnLine1.addStretch(1)
        
        self.vboxAppBtnLine2 = QHBoxLayout()
        self.vboxAppBtnLine2.addStretch(1)
        self.vboxAppBtnLine2.addLayout(self.vboxCameraApp)
        self.vboxAppBtnLine2.addStretch(1)
        self.vboxAppBtnLine2.addLayout(self.vboxMapApp)
        self.vboxAppBtnLine2.addStretch(1)
        self.vboxAppBtnLine2.addLayout(self.vboxCarInfoApp)
        self.vboxAppBtnLine2.addStretch(1)

        self.mainLayout.addStretch(1)
        self.mainLayout.addLayout(self.vboxAppBtnLine1)
        self.mainLayout.addStretch(1)
        self.mainLayout.addLayout(self.vboxAppBtnLine2)
        self.mainLayout.addStretch(1)

        # 信号和槽
        self.musicAppBtn.clicked.connect(self.startMusicApp)
        self.musicAppBtn.clicked.connect(self.close) 
        self.videoAppBtn.clicked.connect(self.startVideoApp)
        self.videoAppBtn.clicked.connect(self.close) 
        self.clockAppBtn.clicked.connect(self.startClockTimeApp)
        self.clockAppBtn.clicked.connect(self.close) 
        self.weatherAppBtn.clicked.connect(self.startWeatherShowApp)
        self.weatherAppBtn.clicked.connect(self.close) 
        self.dht11AppBtn.clicked.connect(self.startDHT11ShowApp)
        self.dht11AppBtn.clicked.connect(self.close)

        self.cameraAppBtn.clicked.connect(self.startCameraApp)
        self.cameraAppBtn.clicked.connect(self.close) 
        self.mapAppBtn.clicked.connect(self.startMapApp)
        self.mapAppBtn.clicked.connect(self.close) 
        self.carInfoAppBtn.clicked.connect(self.startCarInfoApp)
        self.carInfoAppBtn.clicked.connect(self.close)

        self.changeBgBtn.clicked.connect(self.changeBackground)
        #self.init_config()
        #print("---> self.bgIdx:",self.bgIdx)
        #self.update()

    def paintEvent(self, event):
        try:
            #print("paintEvent", self.bgIdx)
            palette = QPalette()
            pix = QPixmap("resource/bg/bg_home_"+str(self.bgIdx)+".png").scaled(self.width(),self.height())
            palette.setBrush(QPalette.Background,QBrush(pix))
            self.setPalette(palette)        
        except Exception as e:
            print("Error reading INI file:", str(e))
            
    def startMusicApp(self):
        print("startMusicApp...")
        self.musicPlayer = MusicPlayer()
        self.musicPlayer.show()
        
    def startVideoApp(self):
        print("startVideoApp...")
        self.videoPlayer = VideoPlayer()
        self.videoPlayer.show()
    
    def startClockTimeApp(self):
        print("startClockTimeApp...")
        self.clockTimeWidget = ClockTimeWidget()
        self.clockTimeWidget.show()
        
    def startWeatherShowApp(self):
        print("startWeatherShowApp...")
        self.weatherShow = WeatherShow()
        self.weatherShow.show()
        
    def startDHT11ShowApp(self):
        print("startDHT11ShowApp...")
        self.dht11Show = DHT11Widget()
        self.dht11Show.show()
        
    def startCameraApp(self):
        print("startCameraApp...")
        self.cameraWidget = CameraWidget()
        self.cameraWidget.show()
        
    def startMapApp(self):
        print("startMapApp...")
        self.mapWidget = MapWidget()
        self.mapWidget.show()
        
    def startCarInfoApp(self):
        print("startCarInfoApp...")
        self.carInfoWidget = CarInfoWidget()
        self.carInfoWidget.show()

    def changeBackground(self):
        if self.bgIdx < 2:
            self.bgIdx = self.bgIdx + 1
        else:
            self.bgIdx = 0
        print("changeBackground", self.bgIdx)
        #write_config(self.bgIdx)
        self.update()

        
if __name__ == "__main__":
    app = QApplication(sys.argv)
    gui = mainWindow()
    gui.show()
    sys.exit(app.exec_())

